﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using GlobalHelper;
using IBSModel;

namespace IBSGate
{
    public class DBGate
    {
        public IList<DataTable> GetMulitDetails(IList<int> lineIds)
        {
            IList<DataTable> result = new List<DataTable>();


            #region Get Data

            IList<IBSModel.TimeTableDetail> details  = new List<TimeTableDetail>();

            try
            {
                details = new IBSBLL.TimeTableDetail().GetMulitModelByMainIds(lineIds);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

            #endregion


            result = GetMulitDataTables(details,lineIds);

            return result;
        }

        private IList<DataTable> GetMulitDataTables(IList<IBSModel.TimeTableDetail> details,IList<int> lineIds)
        {
            IList<DataTable> result = new List<DataTable>();

            //1. 将一个包含多个表的集合,拆分成多个只含有一个表的集合
            IList<IList<IBSModel.TimeTableDetail>> mulitDetails = new List<IList<TimeTableDetail>>();

            int linesCount = lineIds.Count;

            for (int i = 0; i < linesCount; i++)
            {
                mulitDetails.Add(new List<TimeTableDetail>());
            }

            int num = 0;
            int currId = details[0].OwnTimeTable;
            foreach (TimeTableDetail detail in details)
            {
                if (detail.OwnTimeTable != currId)
                {
                    currId = detail.OwnTimeTable;
                    num++;
                }

                mulitDetails[num].Add(detail);
            }
            //2. 将每个集合转为DataTable
            foreach (IList<TimeTableDetail> list in mulitDetails)
            {
                result.Add(GetDataTable(list));
            }

            return result;
        }

        private DataTable GetDataTable(IList<TimeTableDetail> details)
        {
            DataTable result = new DataTable();


            #region init

            int mainId = details[0].OwnTimeTable;

            #endregion

            #region main

            int rowAmount;
            int colAmount;
            IBSModel.TimeTable timeTable = new TimeTable();

            try
            {
                timeTable = new IBSBLL.TimeTable().GetModel(mainId);
                rowAmount = timeTable.ShowRowsAmount;
                colAmount = timeTable.ShowColsAmount;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            #endregion

            result = Helper.CreateEmptyDataTableWithString(rowAmount, colAmount);

            //IList<IBSModel.TimeTableDetail> details = new List<TimeTableDetail>();

            try
            {
                details = new IBSBLL.TimeTableDetail().GetModelByMainId(mainId);

                foreach (TimeTableDetail detail in details)
                {
                    result.Rows[detail.RowNUM][detail.ColNUM] = detail.Content;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        public DataTable GetDetailInfo(int mainId)
        {
            DataTable result = new DataTable();

            #region main

            int rowAmount;
            int colAmount;
            IBSModel.TimeTable timeTable = new TimeTable();

            try
            {
                timeTable = new IBSBLL.TimeTable().GetModel(mainId);
                rowAmount = timeTable.ShowRowsAmount;
                colAmount = timeTable.ShowColsAmount;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            #endregion

            result =  Helper.CreateEmptyDataTableWithString(rowAmount, colAmount);

            IList<IBSModel.TimeTableDetail> details = new List<TimeTableDetail>();

            try
            {
                details = new IBSBLL.TimeTableDetail().GetModelByMainId(mainId);

                foreach (TimeTableDetail detail in details)
                {
                    result.Rows[detail.RowNUM][detail.ColNUM] = detail.Content;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        /// <summary>
        /// 插入一条时刻表明细记录
        /// </summary>
        /// <param name="mainId"></param>
        /// <param name="rowNum"></param>
        /// <param name="colNum"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public void CreateBusScheduleDetailInfo(int mainId, int rowNum, int colNum, string content)
        {
            try
            {
                IBSModel.TimeTableDetail detail = new TimeTableDetail();
                detail.OwnTimeTable = mainId;
                detail.RowNUM = rowNum;
                detail.ColNUM = colNum;
                detail.Content = content;

                new IBSBLL.TimeTableDetail().Add(detail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 更新时刻表主记录中的
        /// 1. 路牌数
        /// 2. 周转数
        /// 3. 显示行数
        /// </summary>
        /// <param name="mainInfoId"></param>
        /// <param name="tokenAmount"></param>
        /// <param name="turnAmount"></param>
        /// <param name="showRowAmount"></param>
        public void UpdateBusScheduleMainInfoWithTokenAndTurnAmount(int mainInfoId, int tokenAmount, int turnAmount, int showRowAmount, int showColAmount)
        {
            IBSBLL.TimeTable bll = new IBSBLL.TimeTable();

            IBSModel.TimeTable mainInfo = bll.GetModel(mainInfoId);
            mainInfo.TokenAmount = tokenAmount;
            mainInfo.TurnAmount = turnAmount;
            mainInfo.ShowRowsAmount = showRowAmount;
            mainInfo.ShowColsAmount = showColAmount;

            bll.Update(mainInfo);
        }

        /// <summary>
        /// 新建时刻表基本信息
        /// </summary>
        /// <param name="lineId"></param>
        /// <param name="ibdmId"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="name"></param>
        /// <param name="valid"></param>
        /// <param name="level"></param>
        /// <param name="type"></param>
        /// <returns>时刻表编号</returns>
        public string CreateBusScheduleMainInfo(int lineId, int ibdmId,
            DateTime begin, DateTime end, string name,
            bool valid, int level, string type)
        {
            string result = string.Empty;

            IBSModel.TimeTable timeTable = new TimeTable();
            timeTable.OwnLineIBDMID = lineId.ToString();
            timeTable.BeginDate = begin;
            timeTable.EndDate = end;
            timeTable.TimeTableName = name;
            timeTable.Valid = valid;
            timeTable.Level = level;
            timeTable.Type = type;

            IList<IBSModel.TimeTable> timeTables = new List<TimeTable>();

            try
            {
                #region disable original table

                DisableOriTables(timeTable);

                #endregion

                IBSBLL.TimeTable timeTableBll = new IBSBLL.TimeTable();
                timeTableBll.Add(timeTable);

                timeTables = timeTableBll.GetList();
            }
            catch (Exception ex)
            {
                throw ex;
            }



            foreach (TimeTable table in timeTables)
            {
                if (table.OwnLineIBDMID == timeTable.OwnLineIBDMID
                    && table.TimeTableName.Trim() == timeTable.TimeTableName.Trim()
                    && table.Type == timeTable.Type
                    && table.Level == timeTable.Level
                    && table.Valid == timeTable.Valid)
                {
                    result = table.ID.ToString();
                    break;
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                throw new Exception("数据库插入数据失败,在时刻表主记录处.");
            }
            return result;
        }

        /// <summary>
        /// 新建时刻表基本信息
        /// </summary>
        /// <param name="lineId"></param>
        /// <param name="ibdmId"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="name"></param>
        /// <param name="valid"></param>
        /// <param name="level"></param>
        /// <param name="type"></param>
        /// <param name="beginStation"></param>
        /// <param name="endStation"></param>
        /// <returns>时刻表编号</returns>
        public string CreateBusScheduleMainInfo(int lineId, int ibdmId,
            DateTime begin, DateTime end, string name,
            bool valid, int level, string type
            ,string beginStation,string endStation)
        {
            string result = string.Empty;

            IBSModel.TimeTable timeTable = new TimeTable();
            timeTable.OwnLineIBDMID = lineId.ToString();
            timeTable.BeginDate = begin;
            timeTable.EndDate = end;
            timeTable.TimeTableName = name;
            timeTable.Valid = valid;
            timeTable.Level = level;
            timeTable.Type = type;
            timeTable.StartStationName = beginStation;
            timeTable.EndStationName = endStation;
            timeTable.CreateDate = DateTime.Now;

            IList<IBSModel.TimeTable> timeTables = new List<TimeTable>();

            try
            {
                #region disable original table

                DisableOriTables(timeTable);

                #endregion

                IBSBLL.TimeTable timeTableBll = new IBSBLL.TimeTable();
                timeTableBll.Add(timeTable);

                timeTables = timeTableBll.GetList();
            }
            catch (Exception ex)
            {
                throw ex;
            }



            foreach (TimeTable table in timeTables)
            {
                if (table.OwnLineIBDMID == timeTable.OwnLineIBDMID
                    && table.TimeTableName.Trim() == timeTable.TimeTableName.Trim()
                    && table.Type == timeTable.Type
                    && table.Level == timeTable.Level
                    && table.Valid == timeTable.Valid)
                {
                    result = table.ID.ToString();
                    break;
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                throw new Exception("数据库插入数据失败,在时刻表主记录处.");
            }
            return result;
        }

        /// <summary>
        /// 禁用原有的时刻表
        /// </summary>
        /// <param name="timeTable"></param>
        private void DisableOriTables(TimeTable timeTable)
        {
            IList<IBSModel.TimeTable> originalTables = new IBSBLL.TimeTable().GetList();

            IList<IBSModel.TimeTable> disableList = new List<TimeTable>();

            //find table need disable
            foreach (TimeTable table in originalTables)
            {
                if (table.OwnLineIBDMID == timeTable.OwnLineIBDMID
                    && table.TimeTableName.Trim() == timeTable.TimeTableName.Trim()
                    && table.Type == timeTable.Type
                    && table.Level == timeTable.Level
                    && table.Valid == timeTable.Valid)
                {
                    disableList.Add(table);
                }
            }

            //disable
            foreach (TimeTable table in disableList)
            {
                table.Valid = false;
                new IBSBLL.TimeTable().Update(table);
            }
        }
    }
}
